﻿@using StudentManagement.Extensions.Pagenation;
@model TeacherListViewModel


<h3>@ViewBag.PageTitle</h3>

<form asp-action="Index" method="get">
    <div class="form-actions no-color">
        <p>
            请输入筛选条件:
            <input type="text" name="searchString" value="@ViewBag.FilterString" />
            <input type="submit" value="查询" class="btn btn-primary btn-sm" />
            <a asp-action="Index" class="btn btn-outline-dark btn-sm">显示全部教师</a>
            <a asp-action="Create" class="btn btn-outline-dark btn-sm">添加教师</a>
        </p>
    </div>
</form>

<div class="table-responsive-sm">
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th scope="col" class="sort-highlight">
                    <a asp-action="Index"
                       asp-route-sortby="Name desc"
                       asp-route-searchString="@ViewBag.FilterString">
                        姓名
                    </a>
                </th>
                <th>
                    <a asp-action="Index"
                       asp-route-sortby="Department desc"
                       asp-route-searchString="@ViewBag.FilterString">
                        院系
                    </a>
                </th>
                <th>
                    <a asp-action="Index"
                       asp-route-sortby="Email desc"
                       asp-route-searchString="@ViewBag.FilterString">
                        电子邮箱
                    </a>
                </th>
                <th>入职时间</th>
                <th>操作</th>
            </tr>
        </thead>

        @if (Model.Teachers.DataSource.Any()) {
            <tbody>
                @foreach (var teacher in Model.Teachers.DataSource) {
                    string selectedRow = "";
                    if (teacher.Id == Model.SelectedId) {
                        selectedRow = "table-success";
                    }
                    <tr class="@selectedRow">
                        <td>@teacher.Id</td>
                        <td>@teacher.Name</td>
                        <td>@teacher.Department</td>
                        <td>@teacher.Email</td>
                        <td>@Html.DisplayFor(modelItem => teacher.HireDate)</td>
                        <td>
                            <a class="btn btn-primary btn-sm" asp-action="details" asp-route-id="@teacher.Id">明细</a>
                            <a class="btn btn-primary btn-sm" asp-action="edit" asp-route-id="@teacher.Id">编辑</a>
                            <a class="btn btn-danger btn-sm" asp-action="delete" asp-route-id="@teacher.Id">删除</a>
                            <a class="btn btn-info btn-sm" asp-action="index" asp-route-selectedId="@teacher.Id">教授课程</a>
                        </td>
                    </tr>
                }
            </tbody>
        }
    </table>

    <div>
        <ul class="pagination">

            <li class="page-item @(!Model.Teachers.HasPrev? "disabled":"")">
                <a asp-route-PageIndex="1"
                   asp-route-sortBy="@ViewBag.SortBy"
                   asp-route-searchString="@ViewBag.SearchString"
                   class="page-link">
                    <i class="fa fa-fast-backward"></i>
                </a>
            </li>

            <li class="page-item @(!Model.Teachers.HasPrev? "disabled":"")">
                <a asp-route-PageIndex="@(Model.Teachers.PageIndex -1)"
                   asp-route-sortBy="@ViewBag.SortBy"
                   asp-route-searchString="@ViewBag.FilterString"
                   class="page-link">
                    <i class="fa fa-step-backward"></i>
                </a>
            </li>

            <!--显示页码-->
            @for (var i = 1; i <= Model.Teachers.PageCount; i++) {
                <li class="page-item @(i == Model.Teachers.PageIndex ? "active" : "")">
                    <a asp-route-PageIndex="@i"
                       asp-route-sortBy="@ViewBag.SortBy"
                       asp-route-searchString="@ViewBag.FilterString"
                       class="page-link">@i</a>
                </li>
            }

            <li class="page-item  @(!Model.Teachers.HasNext? "disabled":"")">
                <a asp-route-PageIndex="@(Model.Teachers.PageIndex + 1)"
                   asp-route-sortBy="@ViewBag.SortBy"
                   asp-route-searchString="@ViewBag.FilterString"
                   class="page-link">
                    <i class="fa fa-step-forward"></i>
                </a>
            </li>

            <li class="page-item  @(!Model.Teachers.HasNext? "disabled":"")">
                <a asp-route-PageIndex="@Model.Teachers.PageCount"
                   asp-route-sortBy="@ViewBag.SortBy"
                   asp-route-searchString="@ViewBag.FilterString"
                   class="page-link">
                    <i class="fa fa-fast-forward"></i>
                </a>
            </li>
        </ul>
    </div>
</div>

<div class="mt-2 mb-4">
    @if (Model.Courses != null) {
        if (Model.Courses.Count > 0) {
            <div class="card mb-3">
                <div class="card-header">
                    该教师正在教授的课程：
                </div>
                <div class="card-body">
                    <table class="table table-bordered">
                        <tr>
                            <th>课程编号</th>
                            <th>课程名称</th>
                            <th>学分</th>
                        </tr>

                        @foreach (var item in Model.Courses) {
                            <tr>
                                <td>@item.CourseNo</td>
                                <td>@item.Title</td>
                                <td>@item.Credits</td>
                            </tr>
                        }
                    </table>
                </div>
            </div>

        }
        else {
            <div class="card mb-3">
                <div class="card-header">
                    该教师还未分配课程
                </div>
            </div>
        }
    }
</div>